<template>
 <panel-head :route="route"/>
 <!-- 账号信息列表 -->
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="permissions_id" label="所属组别">
      <template #default="scope">
        {{ permissionName(scope.row.permissions_id) }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号" /> 
    <el-table-column prop="active" label="状态">
      <template #default="scope">
            <el-tag type="success" v-if="scope.row.active===1">正常</el-tag>
            <el-tag type="danger" v-if="scope.row.active!==1">异常</el-tag>
      </template>
    </el-table-column>
      <el-table-column prop="create_time" label="创建时间">
      <template #default="scope">
        <div class="my-vertical-align">
            <el-icon><Clock /></el-icon>
            {{ dayjs(scope.row.create_time).format('YYYY-MM-DD') }}
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="" label="操作">
      <template #default="scope">
        <el-button type="primary" size="small" @click="openDialog(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

<!-- 编辑弹窗 -->
 <el-dialog v-model="dialogFormVisible" title="编辑用户" width="500">
    <el-form 
    ref="ruleFormRef"
    :model="dialogForm"
    :rules="rules"
    >
      <el-form-item label="id" v-show="false" prop="id">
        <el-input v-model="dialogForm.id"/>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dialogForm.mobile" :disabled="true"/>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="dialogForm.name"/>
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select v-model="dialogForm.permissions_id">
          <el-option 
          v-for="(item,index) in menuDropdownData" 
          :label="item.name" 
          :value="item.id"
          :key="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(ruleFormRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 分页 -->
 <div class="pagination-info">
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="paginationData.pageNum"
        :page-size="paginationData.pageSize"
        layout="total, prev, pager, next"
        :total="tableData.total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive,ref } from 'vue';
import {authAdmin,menuSelectlist,updateUser} from '../../../api'
import dayjs from 'dayjs'
import {useRoute} from 'vue-router'

dayjs().format()

// 弹窗
const ruleFormRef = ref()
const dialogFormVisible=ref(false)
const dialogForm=reactive({
  id:'',
  mobile:'',
  name:'',
  permissions_id:'',
})
//当前活跃路由的响应式对象
const route=useRoute()

//弹窗表单校验规则
const rules = reactive({
  userName: { required: true, message: '请输入用户昵称', trigger: 'blur' },
})
//校验表单，提交
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      //表单验证合法，
      updateUser({name:dialogForm.name,permissions_id:dialogForm.permissions_id}).then((data)=>{
        if(data.code===10000){
          ElMessage.success('提交成功！')
          //关闭弹窗
          dialogFormVisible.value=false
          //重新渲染列表
          authAdmin(paginationData).then((data)=>{
            tableData.value=data.data
          })
        }else{
          ElMessage.danger('错误，请稍后再试')
        }
      })
    }else{
      console.log('失败！')
    }
  })
}

//账号列表数据
const tableData=ref({
  list:[],
  total:0
})
//分页数据
const paginationData=reactive({
  pageNum:1,
  pageSize:10
})
const handleCurrentChange=(val)=>{
  paginationData.pageNum=val
  //请求账号列表
  authAdmin(paginationData).then((data)=>{
    tableData.value=data.data
  })
}

//菜单权限下拉数据
const menuDropdownData=ref([])
//菜单权限名称匹配（方法）
const permissionName=(perId)=>{
  const data=menuDropdownData.value.find(element=>perId===element.id)
  //  console.log(data,'1')
  return data?data.name:'超级管理员'
}

onMounted(()=>{
  //获取菜单权限数据（id，name）
  menuSelectlist().then((data)=>{
    menuDropdownData.value=data.data
  })
  //请求账号列表
  authAdmin(paginationData).then((data)=>{
    tableData.value=data.data
  })
  // console.log(route)
})

//打开弹窗，数据渲染
const openDialog=(rowData)=>{
  dialogFormVisible.value=true
  // console.log(userData)
  Object.assign(dialogForm,{id:rowData.id,mobile:rowData.mobile,name:rowData.name,permissions_id:rowData.permissions_id})
}


</script>
<style lang="less" scoped>
  .my-vertical-align {
  display: flex;
  align-items: center;
  gap:5px
}
</style>